#include "spellcheck.h"

using namespace std;

/*
    SPELLCHECK Main Program Code
    Copyright (C) 2013  Ian Duncan

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

string printMisspelled(spelling s);
string correctMisspelled(string w);

string correctMisspelled(string w, string dict)
{
	string c = correctSpelling(w, dict);
 
        if (c != "")
            return "Did you mean: " + std::string(c) + "?";
        else
            return "No corrections avaiable";

}

void printMisspelled(spelling s, string sepchar, string dict)
{

	string words = s.badList;	

	size_t current;
	size_t next = std::string::npos;
	do {
  		current = next + 1;
  		next = words.find_first_of("\n", current);
  		cout << "" << words.substr(current, next - current) << " - " << correctMisspelled(words.substr(current, next - current), dict) << endl;
	} while (next != std::string::npos);

}

int main(int argc, char *argv[])
{
	string yn;
	
	char *dictionary = "/etc/english.dict"; //Default Dictionary
	spelling s; //Spelling class instance
	bool doAdd = false; //Should we add a word to the dictionary
	bool customDict = false;
	
	
	
	
	//Check if we have the right number of arguments

	if(argc < 2)
	{
		do_help_msg(argv[0]);
		exit(1);
	}

	//Check for arguments about license and about
	if(string(argv[1]) == "-add")
	{
		doAdd = true;
	}
	if(string(argv[1]) == "-version")
	{
		do_about_msg();
		exit(0);
	}
	if(string(argv[1]) == "-license")
	{
		do_license_msg();
		exit(0);
	}
	if(string(argv[1]) == "-help")
	{
		do_help_msg(argv[0]);
		exit(0);
	}

	try
	{
		if(string(argv[2]) == "-dict")
		{
			dictionary = argv[3];
			customDict = true;
		}

		if(string(argv[3]) == "-dict")
		{
			dictionary = argv[4];
			customDict = true;
		}
	}
	catch(...)
	{
		dictionary = "/etc/english.dict";
	}

	

	if(doAdd == true)
	{
		try
		{
			
			
			add_word(dictionary, argv[2]);

			if(customDict == true)
			{
				cout << "Added word \'" << argv[2] << "\' to the dictionary \'" << dictionary << "\'" << endl;
			}
			else
			{
				cout << "Added word \'" << argv[2] << "\' to the default dictionary (english.dict)" << endl;
			}
		}
		catch(int n)
		{
			if(n == 1)
			{
				cout << "Specified dictionary doesn't exist!" << endl;
			}
		}
	}
	else
	{
		
		
	

	//Now its time to do the spell check
	try
	{
		std::string wordsToCheck(argv[1]);

		s = check_spelling_string(dictionary, wordsToCheck, "\n");

		if(s.badWords == 0)
		{
			cout << "All words spelled correctly!" << endl;
		}
		else
		{

			if(s.badWords == 1)
			{
				cout << s.badWords << " word was spelled incorrectly." << endl << endl;
			}
			else
			{
				cout << s.badWords << " words were spelled incorrectly." << endl << endl;
			}
			cout << "The following words were misspelled:" << endl << endl;
			
			string dict = dictionary;
			
			printMisspelled(s, "\n", dict);
			
			cout << endl;
			cout << "If you would like to add the words above, use:" << endl << endl;
			cout << argv[0] << " -add [word you'd like to add] (Adds a word to the default dictionary english.dict)" << endl;
			cout << argv[0] << " -add [word you'd like to add] -dict [dictionary file] (Adds a word to the dictionary file specified)" << endl;
			cout << endl;
		}
		
	
	}
	catch(int n)
	{
		if(n == 1)
		{
			cout << "Specified dictionary doesn't exist!" << endl;
		}
	}
}

	return 0;
}

	
